package com.youlu.campus.service.form.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.form.SingleFormQuestion;
import com.youlu.campus.service.form.SingleFormQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SingleFormQuestionServiceImpl implements SingleFormQuestionService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public SingleFormQuestion create(SingleFormQuestion req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        req = mongoTemplate.insert(req);
        log.info(":>>> 创建的表单项为:{}", JSON.toJSON(req));
        return req;
    }

    @Override
    public boolean update(SingleFormQuestion req) {
        log.info(":>>> 开始修改表单项:{}",JSON.toJSON(req));
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(req.getId()));
        Update update = new Update();
        boolean toUpdate = false;
        if (StringUtils.isNotBlank(req.getFormTaskConfigId())) {
            update.set("formTaskConfigId", req.getFormTaskConfigId());
            toUpdate = true;
        }
        if (Objects.nonNull(req.getNo())) {
            update.set("no", req.getNo());
            toUpdate = true;
        }
        if (Objects.nonNull(req.getHasStandardAnswer())) {
            update.set("hasStandardAnswer", req.getHasStandardAnswer());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getTitle())) {
            update.set("title", req.getTitle());
            toUpdate = true;
        }
        update.set("imageUrl", req.getImageUrl());
        if (StringUtils.isNotBlank(req.getDetail())) {
            update.set("detail", req.getDetail());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getType())) {
            update.set("type", req.getType());
            toUpdate = true;
        }
        if (Objects.nonNull(req.getRequired())) {
            update.set("required", req.getRequired());
            toUpdate = true;
        }
        if (StringUtils.isNotBlank(req.getValueType())) {
            update.set("valueType", req.getValueType());
            toUpdate = true;
        }
        if (Objects.nonNull(req.getTaskCount())) {
            update.set("taskCount", req.getTaskCount());
            toUpdate = true;
        }
        if (toUpdate) {
            update.set("updatedTime", new Date());
            return mongoTemplate.updateFirst(query, update, SingleFormQuestion.class).getModifiedCount() > 0 ? true : false;
        }
        return true;
    }

    @Override
    public SingleFormQuestion findOne(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, SingleFormQuestion.class);
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("deleted", Boolean.TRUE);
        update.set("updatedTime", new Date());
        return mongoTemplate.updateFirst(query, update, SingleFormQuestion.class).getModifiedCount() > 0 ? true : false;
    }

    @Override
    public List<SingleFormQuestion> findByFormTaskConfigId(String formTaskConfigId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("formTaskConfigId").is(formTaskConfigId));
        query.addCriteria(Criteria.where("deleted").ne(Boolean.TRUE));
        Sort sort = Sort.by(Sort.Direction.ASC, "no");
        query.with(sort);
        return mongoTemplate.find(query, SingleFormQuestion.class);
    }
}
